//
//  ResetPasswordNextViewController.m
//  HYApp
//
//  Created by Jianbo Cai on 2020/7/24.
//  Copyright © 2020 Unidax. All rights reserved.
//

#import "ResetPasswordNextViewController.h"
#import <JFBase/UITextField+LimitLength.h>
#import "ChangeVerifyView.h"


@interface ResetPasswordNextViewController ()

@property (weak, nonatomic) IBOutlet UITextField *oldPassword;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UIButton *resetBtn;
- (IBAction)resetAction:(id)sender;

@end

@implementation ResetPasswordNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.oldPassword setLeftImageName:@"password_icon"];
    [self.password setLeftImageName:@"password_icon"];
    self.navigationItem.title = JFMainLocalizedString(@"myProfile.resetPwd");
    self.resetBtn.AWtitle(JFMainLocalizedString(@"myProfile.submit"), UIControlStateNormal);
    
    self.oldPassword.placeholder = JFMainLocalizedString(@"myProfile.resetPwd");
    self.password.placeholder = JFMainLocalizedString(@"myProfile.confirmPwd");
    
    [self.oldPassword addSecretButton];
    [self.password addSecretButton];
}



- (IBAction)resetAction:(id)sender {
    if ([JFStringHelper isNotEmpty:self.oldPassword.text] && [JFStringHelper isNotEmpty:self.password.text]) {
        if([self.oldPassword.text isEqualToString:self.password.text]){
            [self checkPassword:self.password];
        }else{
             [SLRemindView showMessage:JFMainLocalizedString(@"passwords_different_tip") toView:self.view];
        }
    }else{
        [SLRemindView showMessage:@"不能為空" toView:self.view];
    }
    
}

- (void)checkPassword:(UITextField *)textField {
    NSString *message = [NSString stringWithFormat:@"%@\n"
                         "%@\n"
                         "• %@\n"
                         "• %@\n"
                         "• %@\n"
                         "• %@", JFMainLocalizedString(@"pass_word_tip_length"), JFMainLocalizedString(@"pass_word_tip_type"), JFMainLocalizedString(@"pass_word_tip_number"), JFMainLocalizedString(@"pass_word_tip_upper"), JFMainLocalizedString(@"pass_word_tip_lower"), JFMainLocalizedString(@"pass_word_tip_special")];
    
    BOOL hasNumChar = NO;
    BOOL hasLowerCaseChar = NO;
    BOOL hasUpperCaseChar = NO;
    BOOL hasSpeciaChar = NO;
    NSInteger max = 4;
    
    for (int i = 0; i < textField.text.length; i++) {
        unichar charStr = [textField.text characterAtIndex:i];
        
        if (charStr >= '0' && charStr <= '9') {
            if(!hasNumChar){
               max-=1;
            }
            hasNumChar = YES;
           
        } else if (charStr >= 'a' && charStr <= 'z') {
            if(!hasLowerCaseChar){
                max-=1;
            }
            hasLowerCaseChar = YES;
        } else if (charStr >= 'A' && charStr <= 'Z') {
            if(!hasUpperCaseChar){
                max-=1;
            }
            hasUpperCaseChar = YES;
        } else {
            if(!hasSpeciaChar){
                max-=1;
            }
            hasSpeciaChar = YES;
        }
    }
    

    
    if (max <= 1) {
        
        ChangeVerifyView *view = [[[NSBundle mainBundle] loadNibNamed:@"ChangeVerifyView" owner:nil options:nil] firstObject];
        view.frame = CGRectMake(0, 0, CUSTOMVIEW_WIDTH, 110);
        FZAlertController *vc = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"Captcha_ico_b"] title:nil customView:view message:nil];
        vc.showCloseBtn = YES;
        WEAKSELF
        FZAlertAction *action = [FZAlertAction actionWithTitle:JFMainLocalizedString(@"confirm") style:UIAlertActionStyleDefault handler:^(FZAlertAction *action) {
            NSString * verifyType  = view.verifyType;
            NSString * token  = view.codeText.text;
            [HYNetHelper resetPwd:textField.text verifyType:verifyType token:token callback:^(JFResultVo *resultVo) {
//                if (resultVo.errorNo == 0 && [resultVo.results boolForKey:@"result"]) {
//                    [SLRemindView showMessage:[resultVo.results stringForKey:@"message"] toView:weakSelf.view];
//                }else{
//                    [SLRemindView showMessage:@"修改成功" toView:weakSelf.view];
//                }
                [SLRemindView showMessage:[resultVo.results stringForKey:@"message"] toView:weakSelf.view];
            }];
        }];
        
        [vc addAction:action];
        [self presentViewController:vc animated:YES completion:nil];
        
        //弹出认证框

    }else{
        NSMutableParagraphStyle * paragraph = [[NSMutableParagraphStyle alloc]init];
        paragraph.lineSpacing = 7.0;
        paragraph.lineBreakMode = NSLineBreakByTruncatingMiddle;
        
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:message attributes:@{NSForegroundColorAttributeName : UIColorFromHex(0xD32B10), NSParagraphStyleAttributeName : paragraph}];
        
        UIColor *hasColor = UIColorFromHex(0xF8C028);
        UIColor *noColor = UIColorFromHex(0x333333);
        
        NSArray *array = @[JFMainLocalizedString(@"pass_word_tip_number"), JFMainLocalizedString(@"pass_word_tip_upper"), JFMainLocalizedString(@"pass_word_tip_lower"), JFMainLocalizedString(@"pass_word_tip_special")];
        for (int i = 0; i < 4; i++) {
            NSRange rang = [message rangeOfString:array[i]];
            rang = NSMakeRange(rang.location - 2, rang.length + 2);
            switch (i) {
                case 0:
                    [attributedString addAttribute:NSForegroundColorAttributeName value:hasNumChar ? hasColor : noColor range:rang];
                    break;
                case 1:
                    [attributedString addAttribute:NSForegroundColorAttributeName value:hasUpperCaseChar ? hasColor : noColor range:rang];
                    break;
                case 2:
                    [attributedString addAttribute:NSForegroundColorAttributeName value:hasLowerCaseChar ? hasColor : noColor range:rang];
                    break;
                case 3:
                    [attributedString addAttribute:NSForegroundColorAttributeName value:hasSpeciaChar ? hasColor : noColor range:rang];
                    break;
                    
                default:
                    break;
            }
        }
        FZAlertController *alertController = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"prompt_icon"] title:message customView:nil message:nil];
        alertController.showCloseBtn = YES;
        alertController.messageAttStr = attributedString;
        alertController.messageAlignment = NSTextAlignmentLeft;
        [self presentViewController:alertController animated:YES completion:nil];
        
    }

}



@end
